<?php
/* sunsblog.net functions.inc (c)2004-2007 Gjokica Zafirovski */

// globals
// zapisi jazik vo coockie, proveri dali postoi i definiraj defauolt ako ne postoi

session_cache_limiter('public');


if($jazik) {
    $web_lang = $jazik;
    setcookie("sb_web_lang", $web_lang, time()+100000, "/", "", 0);
} else if ($HTTP_COOKIE_VARS['sb_web_lang']) {
    $web_lang = $HTTP_COOKIE_VARS['sb_web_lang'];
} else {
    $web_lang = 'mk';
}


// setiraj jazik tabeli

if ($web_lang == 'mk') {
  $tbl_lokacija = 'lokacija';
  $tbl_sodrzina = 'sodrzina';
 } else {
  $tbl_lokacija = 'lokacija_'.$web_lang;
  $tbl_sodrzina = 'sodrzina_'.$web_lang;
}

// db variables

$dbhost = "localhost";
$dbname = "sunsblog";
$dbuser = "sunsblog";
// $uploaddir = "/hd2/wwwtest/sunsblog/images/";
$uploaddir = "/data/13/1/99/158/1262647/user/1347118/htdocs/sunsblog/images/";
$datumot = date("Y-m-d");

// otvori mysql konekcija
function openConnection() {
	global $dbhost, $dbuser, $dbname, $db;
    $db = mysql_connect($dbhost,$dbuser,'SunSblog2004')
      or die("Could not connect to DB!");

mysql_query("SET NAMES 'utf8'");

mysql_select_db('sunsblog',$db)
    or die ("Could not select DB!");

}

// zatvori mysql konekcija
function closeConnection($db) {
  mysql_close($db);
}

// funkcija za kreiranje kalendar
function build_calendar($month,$year,$dateArray) {
 global $monthName;
 // die($month.' '.$dateArray[1]);
  // Create array containing abbreviations of days of week.
  $daysOfWeek = array('Не','По','Вт','Ср','Че','Пе','Са');

  // What is the first day of the month in question?
  $firstDayOfMonth = mktime(0,0,0,$month,1,$year);

  // How many days does this month contain?
  $numberDays = date('t',$firstDayOfMonth);

  // Retrieve some information about the first day of the
  // month in question.
  $dateComponents = getdate($firstDayOfMonth);

  // What is the name of the month in question?
  $monthName = $dateComponents['month'];
  makMesec($monthName);
  // What is the index value (0-6) of the first day of the
  // month in question.
  $dayOfWeek = $dateComponents['wday'];

  // Create the table tag opener and day headers
  $prevmonth = $month - 1;
  $nextmonth = $month + 1;
  $prevyear = $year;
  $nextyear = $year;
  if ($prevmonth == '0'){
      $prevmonth = '12';
      $prevyear = $year - 1;
      $nextyear = $year;
  } else if ($nextmonth == '13') {
      $nextmonth = '1';
      $prevyear = $year;
      $nextyear = $year + 1;
  }
  $calendar = "<table class='calendar' align='center'>";
  //die($_HTTP_REFERER);
  $calendar .= "<caption><a href='?month=$prevmonth&year=$prevyear'>&lt;</a>&nbsp;$monthName, $year <a href='?month=$nextmonth&year=$nextyear'>&gt;</a></caption>";
  $calendar .= "<tr>";

  // Create the calendar headers

  foreach($daysOfWeek as $day) {
    $calendar .= "<th class='header'>$day</th>";
  }

  // Create the rest of the calendar

  // Initiate the day counter, starting with the 1st.

  $currentDay = 1;

  $calendar .= "</tr><tr>";

  // The variable $dayOfWeek is used to
  // ensure that the calendar
  // display consists of exactly 7 columns.

  if ($dayOfWeek > 0) {
    $calendar .= "<td colspan='$dayOfWeek'>&nbsp;</td>";
  }

  while ($currentDay <= $numberDays) {

    // Seventh column (Saturday) reached. Start a new row.

    if ($dayOfWeek == 7) {

      $dayOfWeek = 0;
      $calendar .= "</tr><tr>";

    }

    // Is the $currentDay a member of $dateArray? If so,
    // the day should be linked.
    if($month < 10) {
      $theMonth = "0".$month;
    } else {
      $theMonth = $month;
    }
    if($currentDay < 10) {
      $theDay = "0".$currentDay;
    } else {
      $theDay = $currentDay;
    }
    $checkDate = $year."-".$theMonth."-".$theDay;
    //die($checkDate."  ".$dateArray[1]);
    if (in_array($checkDate,$dateArray)) {

      $date = "$year-$month-$currentDay";

      $calendar .= "<td class='linkedday'> 
                       <a href='blog.php?date=$date&month=$month&year=$year' 
                       class='calendarlink'>$currentDay</a></td>";

      // $currentDay is not a member of $dateArray. 

    } else { 

      $calendar .= "<td class='day'>$currentDay</td>"; 

    } 

    // Increment counters 
  
    $currentDay++; 
    $dayOfWeek++;

  } 

  // Complete the row of the last week in month, if necessary 

  if ($dayOfWeek != 7) {  
      
    $remainingDays = 7 - $dayOfWeek; 
    $calendar .= "<td colspan='$remainingDays'>&nbsp;</td>";  

  } 

  $calendar .= "</table>"; 

  return $calendar;

}

// prati obicen tekst email
function sendmail($to, $subject, $message, $from) {
  mail("$to","$subject","$message","FROM:$from");
}

// mk lokalizacija na imeto na mesecite
function makMesec($monthName) {
	global $monthName;
	if ($monthName == 'January') {
		$monthName = 'Јануари';
	}
	else if ($monthName == 'February') {
		$monthName = 'Фебруари';
	}
	else if ($monthName == 'March') {
		$monthName = 'Март';
	}
	else if ($monthName == 'April') {
		$monthName = 'Април';
	}
	else if ($monthName == 'May') {
		$monthName = 'Мај';
	}
	else if ($monthName == 'June') {
		$monthName = 'Јуни';
	}
	else if ($monthName == 'July') {
		$monthName = 'Јули';
	}
	else if ($monthName == 'August') {
		$monthName = 'Август';
	}
	else if ($monthName == 'September') {
		$monthName = 'Септември';
	}
	else if ($monthName == 'October') {
		$monthName = 'Октомври';
	}
	else if ($monthName == 'November') {
		$monthName = 'Ноември';
	}
	else if ($monthName == 'December') {
		$monthName = 'Декември';
	}
	//die($monthName);
}

// funkcija za pisuvanje ubav datum
function datum($date) {
  if ($date == "0000-00-00") return "";
  if (strlen($date)==10) {
    $d = array();
    $d[day] = substr($date, 8, 2);
    $d[month] = substr($date, 5, 2);
    $d[year] = substr($date, 0, 4);
    return $d[day].'.'.$d[month].'.'.$d[year];
  } else {
    $d = array();
    $d[day] = substr($date, 7, 2);
    $d[month] = substr($date, 5, 1);
    $d[year] = substr($date, 0, 4);
    return $d[day].'.'.$d[month].'.'.$d[year];
  }
}

?>
